package com.itheima.wms.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pinda.exception.BizException;
import com.itheima.pinda.utils.SpringUtils;
import io.swagger.annotations.ApiModel;

import java.io.Serializable;
import java.util.List;

public class RelationUtils {

    /**
     * 校验是否有关联
     *
     * @param id
     * @param column
     * @param service
     */
    public static void checkRelation(Serializable id, String column, List<Class> service) {
        BaseMapper baseMapper;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(column, id);
        wrapper.last("limit 1");
        for (Class name : service) {
            baseMapper = (BaseMapper) SpringUtils.getBean(name);
            Object entity = baseMapper.selectOne(wrapper);
            if (null != entity) {
                Class<? extends Object> clazz = entity.getClass();
                ApiModel annotation = clazz.getAnnotation(ApiModel.class);
                throw new BizException(annotation.description() + "占用，无法删除");
            }
        }
    }
}
